package com.chinasoft.ui;

import javax.sound.sampled.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.LineBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class PlayView extends JFrame implements ActionListener {

    JButton replayBtn, saveBtn, nextBtn, prevBtn, exitBtn, loadBtn, musicBtn;
    JLabel levelLabel;
    JPanel btnArea;

    GamePanel gamePanel;
    MyThread musicThread;
    boolean musicEnabled = false;

    public PlayView() {
//        frame.
        setTitle("开始play game ！");
        setBounds(5, 5, 800, 642);

        JPanel contentPane = new JPanel();
//        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setBorder(new LineBorder(Color.GREEN));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        //  游戏区域
        gamePanel = new GamePanel();
        contentPane.add(gamePanel);

        // 开关区域
        btnArea = new JPanel();
        btnArea.setBackground(Color.LIGHT_GRAY);
        btnArea.setBounds(602, 0, 180, 600);
        btnArea.setVisible(true);
        // 非常重要
        btnArea.setLayout(null);
        btnArea.setFont(new Font("微软雅黑", Font.PLAIN, 20));
        contentPane.add(btnArea);

        // 显示第几关
        levelLabel = new JLabel("第  " + gamePanel.getMapData().getLevel() + "  关 老师版本");
        levelLabel.setBounds(40, 40, 150, 25);
        levelLabel.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(levelLabel);

        // 添加 按钮
        replayBtn = new JButton("重  来");
        replayBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(replayBtn);
        replayBtn.setBounds(40, 100, 100, 25);
        replayBtn.addActionListener(this);
        saveBtn = new JButton("保  存");
        saveBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(saveBtn);
        saveBtn.setBounds(40, 180, 100, 25);
        saveBtn.addActionListener(this);
        prevBtn = new JButton("上一关");
        prevBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(prevBtn);
        prevBtn.setBounds(40, 260, 100, 25);
        prevBtn.addActionListener(this);
        loadBtn = new JButton("读 入");
        loadBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(loadBtn);
        loadBtn.setBounds(40, 370, 100, 25);
        loadBtn.addActionListener(this);
        musicBtn = new JButton("音  乐");
        musicBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(musicBtn);
        musicBtn.setBounds(40, 450, 100, 25);
        musicBtn.addActionListener(this);
        exitBtn = new JButton("退  出");
        exitBtn.setFont(new Font("微软雅黑", Font.PLAIN, 15));
        btnArea.add(exitBtn);
        exitBtn.setBounds(40, 530, 100, 25);
        exitBtn.addActionListener(this);
//        contentPane.add(game);

        setVisible(true);
//        musicThread = new MyThread();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == exitBtn) {
            System.out.println("游戏退出了");
            System.exit(0);
        }
//        if (e.getSource() == nextBtn) {
//            if (gamePanel.drawNextLevel() == false) {
//                JOptionPane.showMessageDialog(this, "已经是最后一关了");
//            }
//        }
        if (e.getSource() == saveBtn) {
            gamePanel.getMapData().saveCurrent();
            JOptionPane.showMessageDialog(this, "已经保存成功 ！");
            System.out.println("保存成功");
        }
        if (e.getSource() == replayBtn) {
            gamePanel.drawGotoLevel(gamePanel.getMapData().getLevel());
        }
        if (e.getSource() == prevBtn) {
            int le = gamePanel.getMapData().getLevel();
            levelLabel = new JLabel("第  " + gamePanel.getMapData().getLevel() + "  关");
//            btnArea.repaint();
            repaint();

            if (le == 1) {
                JOptionPane.showMessageDialog(this, "已经是第一关了");
            } else{
                gamePanel.drawGotoLevel(le - 1);
            }
        }
        if (e.getSource() == loadBtn) {
            System.out.println("读入保存的场景");
            gamePanel.drawLoadLevel();
//            gamePanel.drawGotoLevel(gamePanel.getMapData().MAX_LEVEL);
        } else if (e.getSource() == musicBtn) {
            System.out.println(musicEnabled);
            musicEnabled = !musicEnabled;
            if (musicEnabled) {
                musicThread = new MyThread();
                musicThread.start();
            } else {
                System.out.println("stop thread");
                musicThread.stopThread();
//                musicThread = new MyThread();
            }
        }
        // 讲焦点给 game Panel
        gamePanel.requestFocusInWindow();
    }


}
